//
//  LSAccountArchive.m
//  LookScore
//
//  Created by wr on 2018/1/17.
//  Copyright © 2018年 ChongQingXunTiCompany. All rights reserved.
//

#import "LSAccountArchive.h"
#import "LSHttpEnclosureTool.h"

#define LSAutoLoginSuccess  @"autoLoginSuccess"     //自动登录

#define LSAutoLoginValue    @"autoLoginValue"       //是否是自动登录

#define LSUserTokenValue    @"UserTokenValue"       //userToken标示

#define LSApiDomainValue    @"LSApiDomainValue"     //ApiDomain标示

#define LSAccountFileName [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0] stringByAppendingPathComponent:@"account.data"]


@implementation LSAccountArchive

static LSAccountCoder *_accountCoder;

#pragma mark 🔥 保存登录用户信息

+ (BOOL)saveAccount:(LSAccountCoder *)account
{
    return [NSKeyedArchiver archiveRootObject:account toFile:LSAccountFileName];
}

#pragma mark 🔥 获取登录用户信息

+ (LSAccountCoder *)account
{
    if (_accountCoder == nil) {
        
        _accountCoder = [NSKeyedUnarchiver unarchiveObjectWithFile:LSAccountFileName];
    }
    return _accountCoder;
}

#pragma mark 🔥 注销用户信息

+(void)logout{
    //清除当前account
    _accountCoder = nil;
    
    //删除归档文件
    NSFileManager* fileManager=[NSFileManager defaultManager];
    BOOL isSave=[[NSFileManager defaultManager] fileExistsAtPath:LSAccountFileName];
    
    
    if (isSave) {
        
        [fileManager removeItemAtPath:LSAccountFileName error:nil];
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"refreshUserInfo" object:nil];
        
        return ;
    }
    
    
    
    
}

#pragma mark 🔥 判断用户是否需要登陆

+(BOOL)isLogin{
    
    if (_accountCoder == nil || [CommonClass isBlankString:_accountCoder.Cid]||[CommonClass isBlankString:_accountCoder.LoginName]) {
        
        _accountCoder = [NSKeyedUnarchiver unarchiveObjectWithFile:LSAccountFileName];
    }
    if (_accountCoder && ![CommonClass isBlankString:_accountCoder.Cid] && ![CommonClass isBlankString:_accountCoder.LoginName]) {
        return YES;
    }
    return NO;
}



#pragma mark 🔥 是否自动登录
+(void)autoLoginStatus:(BOOL)autoLogin{
    [[NSUserDefaults standardUserDefaults]setBool:autoLogin forKey:LSAutoLoginValue];
    [[NSUserDefaults standardUserDefaults] synchronize];
}


#pragma mark 🔥 用户自动登录成功OR失败
+(void)autoLoginSuccessStatus:(BOOL)autoLogin{
    [[NSUserDefaults standardUserDefaults]setBool:autoLogin forKey:LSAutoLoginSuccess];
    [[NSUserDefaults standardUserDefaults] synchronize];
}
/**
 用户自动登录(status Value)
 
 @return 是否自动登录取值
 */
+(BOOL)autoLoginStatus{
    
   return [[NSUserDefaults standardUserDefaults]boolForKey:LSAutoLoginValue];
    
}

/**
 用户自动登录(status Value SuccessORFail)
 
 @return 保存用户自动登录成功OR失败取值
 */
+(BOOL)autoLoginSuccessStatus{
    
   return [[NSUserDefaults standardUserDefaults]boolForKey:LSAutoLoginSuccess];
}

/**
 保存新的userToken
 
 @param userToken 新的
 */
+(void)saveUserToken:(NSString *)userToken{
    if ([CommonClass isBlankString:userToken]) {
        userToken=@"";
    }
    [[NSUserDefaults standardUserDefaults]setObject:userToken forKey:LSUserTokenValue];
    [[NSUserDefaults standardUserDefaults] synchronize];
}


/**
 获取userToken
 */
+(NSString *)objectUserToken{
    if ([[NSUserDefaults standardUserDefaults]objectForKey:LSUserTokenValue] == nil || [[[NSUserDefaults standardUserDefaults]objectForKey:LSUserTokenValue] isEqualToString:@""]) {
        return @"";
    }
     return [[NSUserDefaults standardUserDefaults]objectForKey:LSUserTokenValue];
}

/**
保存新的ApiDomain

@param ApiDomain ApiDomain
*/
+(void)saveApiDomain:(NSString *)ApiDomain{
    if ([CommonClass isBlankString:ApiDomain]) {
        ApiDomain=LSHostURL;
    }
    [[NSUserDefaults standardUserDefaults]setObject:ApiDomain forKey:LSApiDomainValue];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

/**
 获取userToken
 */
+(NSString *)objectApiDomain{

     return [[NSUserDefaults standardUserDefaults]objectForKey:LSApiDomainValue];
    
}



+ (void)saveEventWith:(NSInteger)eventId{
    NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];
    NSMutableArray * newEventArray = @[].mutableCopy;
    
    NSMutableArray * oldEventArray = [userDefaults objectForKey:@"eventArray"];
    [newEventArray addObject:@(eventId)];
    [newEventArray addObjectsFromArray:oldEventArray];
    [userDefaults setObject:newEventArray forKey:@"eventArray"];
    [userDefaults synchronize];
}

+ (NSMutableArray *)getSaveEventArray{
    
    return [[NSUserDefaults standardUserDefaults] objectForKey:@"eventArray"];
}

+ (void)deletctAllSaveEvent{
    NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];
    NSMutableArray * newEventArray = @[].mutableCopy;
    NSMutableArray * oldEventArray = [userDefaults objectForKey:@"eventArray"];
    [newEventArray addObjectsFromArray:oldEventArray];
    [newEventArray removeAllObjects];
    [userDefaults setObject:newEventArray forKey:@"eventArray"];
    [userDefaults synchronize];
}

+ (void)deletctEventWithEventId:(NSInteger)eventId{
    NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];
//    NSMutableArray * oldEventArray = @[].mutableCopy;
    NSMutableArray * oldEventArray;
    oldEventArray = [userDefaults objectForKey:@"eventArray"];
    NSMutableArray * array = [NSMutableArray arrayWithArray:oldEventArray];
    for (NSString * str in oldEventArray) {
        if (eventId == str.integerValue) {
//            [array addObject:@(eventId)];
            [array removeObject:@(eventId)];
        }
    }
//    [oldEventArray removeObjectsInArray:array];
    [userDefaults setObject:array forKey:@"eventArray"];
    [userDefaults synchronize];
}

@end
